package puyu.manage.service.utils;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
/**
 *  @ClassName: 数据统一返回格式
 *  @author: whl
 *  @Date: 2024/5/28 22:42
 *  @Description: Description of this class
 */
public class R implements Serializable {
    private static final long serialVersionUID = 1005L;
    public static final String CODE_SUCCESS = "200";
    public static final String MSG_SUCCESS = "成功";
    public static final String CODE_FAIL = "500";
    public static final String MSG_FAIL = "失败";
    private String code;
    private String message;
    private boolean success;
    private Object data;

    public R() {
    }

    private R(boolean success) {
        this.success = success;
        if (success) {
            this.code = "200";
            this.message = "成功";
        } else {
            this.code = "500";
            this.message = "失败";
        }

    }

    private R(boolean success, String code) {
        this.success = success;
        this.code = code;
        if (success) {
            this.message = "成功";
        } else {
            this.message = "失败";
        }

    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public Object getData() {
        return this.data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public static R success() {
        return new R(true);
    }

    public static R success(Object resultData) {
        R rr = new R(true);
        rr.setData(resultData);
        return rr;
    }

    public static R success(Object resultData, String message) {
        R rr = new R(true);
        rr.setData(resultData);
        rr.setMessage(message);
        return rr;
    }

    public static R success(Object resultData, String code, String message) {
        R rr = new R(true, code);
        rr.setData(resultData);
        rr.setMessage(message);
        return rr;
    }

    public static R fail() {
        return new R(false);
    }

    public static R fail(String message) {
        R rr = new R(false);
        rr.setMessage(message);
        return rr;
    }

    public static R fail(String code, String message) {
        R rr = new R(false, code);
        rr.setMessage(message);
        return rr;
    }

    public static R fail(String code, String message, Object resultData) {
        R rr = new R(false, code);
        rr.setData(resultData);
        rr.setMessage(message);
        return rr;
    }

    public Map<String, Object> data2Map() {
        return (Map)this.data;
    }

    public List<Map<String, Object>> data2MapList() {
        return (List)this.data;
    }

    public List<String> data2StrList() {
        return (List)this.data;
    }

    public List<Object> data2ObjList() {
        return (List)this.data;
    }
}
